package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @TODO 网关跨越请求设置
 * @author gcq
 * @Create 2020-12-18
 */
@Configuration
public class GulimallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        // 请求来源不限制
        corsConfiguration.addAllowedOrigin("*");
        // 请求方式不限制
        corsConfiguration.addAllowedMethod("*");
        // 请求头不限制
        corsConfiguration.addAllowedHeader("*");
        // 是否支持用户凭据(cookie)
        corsConfiguration.setAllowCredentials(true);
        // 指定模式路径注册
        source.registerCorsConfiguration("/**",corsConfiguration);

        return new CorsWebFilter(source);
    }
}